package com.umeox.iqibla.azkar.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.umeox.iqibla.azkar.R
import com.umeox.iqibla.azkar.db.Azkar
import com.umeox.iqibla.azkar.utils.DateUtils
import com.umeox.moto.watch.themes.widget.TextView

class HistoryListAdapter :
    RecyclerView.Adapter<HistoryListAdapter.ItemViewHolder>() {
    var itemClickListener: ItemClickListener? = null
    private var dataList = mutableListOf<Azkar>()
    private lateinit var context:Context

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_history, parent, false)
        context = parent.context
        return ItemViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val movieBean = dataList[position]
        holder.bind(movieBean)
        holder.itemView.setOnClickListener { itemClickListener?.onItemClicked(movieBean) }
    }

    @SuppressLint("NotifyDataSetChanged")
    fun setDataList(list: List<Azkar>) {
        dataList.clear()
        dataList.addAll(list)
        notifyDataSetChanged()
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private val mTvCount: TextView = itemView.findViewById(R.id.tv_count)
        private val mTvDate: TextView = itemView.findViewById(R.id.tv_date)

        fun bind(azkar: Azkar) {

            mTvCount.text = "${azkar.count}"
            mTvDate.text = DateUtils.formatDateTime(azkar.todayMillis * 1000L,"yyyy/MM/dd")
        }
    }

    interface ItemClickListener {
        fun onItemClicked(azkar: Azkar)
    }
}